const { recharge, handleRechargeCallback, checkPaymentStatus, refundRecharge } = require('../controllers/recharge');
const logger = require('../config/logger');
async function rechargeRoutes(fastify, options) {

  // 用户充值
  fastify.post('/recharge', {
    schema: {
      description: '用户充值',
      tags: ['充值管理'],
      body: {
        type: 'object',
        required: ['amount', 'paymentMethod'],
        properties: {
          amount: { type: 'number', description: '充值金额' },
          paymentMethod: { type: 'string', description: '支付方式' }
        }
      },
      response: {
        200: {
          type: 'object',
          description: '充值记录创建成功',
          properties: {
            id: { type: 'number', description: '充值记录ID' },
            userId: { type: 'number', description: '用户ID' },
            amount: { type: 'number', description: '充值金额' },
            rechargeStatus: { type: 'string', description: '充值状态' },
            qpayInvoiceId: { type: 'string', description: 'QPay发票ID' },
            qrText: { type: 'string', description: '二维码文本' },
            qrImage: { type: 'string', description: '二维码图片URL' },
            paymentUrl: { type: 'string', description: '支付页面URL' },
            urls: { 
              type: 'array',
              description: '支付方式列表',
              items: {
                type: 'object',
                properties: {
                  name: { type: 'string', description: '支付方式名称' },
                  description: { type: 'string', description: '支付方式描述' },
                  logo: { type: 'string', description: '支付方式logo' },
                  link: { type: 'string', description: '支付链接' }
                }
              }
            },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        401: {
          type: 'object',
          description: '未认证',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        404: {
          type: 'object',
          description: '用户不存在',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        500: {
          type: 'object',
          description: '处理失败',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    handler: recharge,
    preHandler: [fastify.authenticate]
  });

  // 充值回调
  fastify.get('/recharge/callback', {
    schema: {
      description: '充值支付回调（由QPay回调）',
      tags: ['充值管理'],
      querystring: {
        type: 'object',
        required: ['timestamp', 'nonce', 'signature', 'rechargeId'],
        properties: {
          timestamp: { type: 'string', description: '时间戳' },
          nonce: { type: 'string', description: '随机数' },
          signature: { type: 'string', description: '签名' },
          rechargeId: { type: 'string', description: '充值记录ID' }
        }
      },
      response: {
        200: { type: 'string', const: 'SUCCESS', description: '回调处理成功' },
        400: { type: 'string', const: 'ERROR', description: '回调处理失败' },
        404: { type: 'string', const: 'ERROR', description: '充值记录不存在' },
        500: { type: 'string', const: 'ERROR', description: '处理失败' }
      }
    },
    handler: handleRechargeCallback
  });

  // 查询充值状态
  fastify.get('/recharge/:rechargeId/status', {
    schema: {
      description: '查询充值支付状态',
      tags: ['充值管理'],
      params: {
        type: 'object',
        required: ['rechargeId'],
        properties: {
          rechargeId: { type: 'string', description: '充值记录ID' }
        }
      },
      response: {
        200: {
          type: 'object',
          description: '查询成功',
          properties: {
            rechargeId: { type: 'number', description: '充值记录ID' },
            status: { type: 'string', description: '支付状态' },
            paymentId: { type: 'string', description: '支付ID' },
            amount: { type: 'number', description: '充值金额' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        401: {
          type: 'object',
          description: '未认证',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        404: {
          type: 'object',
          description: '充值记录不存在',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        500: {
          type: 'object',
          description: '查询失败',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    handler: checkPaymentStatus,
    preHandler: [fastify.authenticate]
  });

  // 充值退款
  fastify.post('/recharge/:rechargeId/refund', {
    schema: {
      description: '充值退款',
      tags: ['充值管理'],
      params: {
        type: 'object',
        required: ['rechargeId'],
        properties: {
          rechargeId: { type: 'string', description: '充值记录ID' }
        }
      },
      body: {
        type: 'object',
        properties: {
          reason: { type: 'string', description: '退款原因' }
        }
      },
      response: {
        200: {
          type: 'object',
          description: '退款成功',
          properties: {
            rechargeId: { type: 'number', description: '充值记录ID' },
            status: { type: 'string', description: '退款状态' },
            amount: { type: 'number', description: '退款金额' },
            refundTime: { type: 'string', format: 'date-time', description: '退款时间' },
            reason: { type: 'string', description: '退款原因' },
            qpayRefundId: { type: 'string', description: 'QPay退款ID' }
          }
        },
        400: {
          type: 'object',
          description: '退款失败',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        401: {
          type: 'object',
          description: '未认证',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        404: {
          type: 'object',
          description: '充值记录不存在',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        },
        500: {
          type: 'object',
          description: '处理失败',
          properties: {
            error: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    handler: refundRecharge,
    preHandler: [fastify.authenticate]
  });

}

module.exports = rechargeRoutes;